#include "UDPServer.hpp"
#include <memory>
#include <stdexcept>
#include "Route.hpp"
#include "ThreadPool.hpp"

using namespace std;
string DefaultHandler(const string &message)
{
    string result = "hahaha";
    return result + message;
}
using task_t = std::function<void()>;
//./udpserver port
int main(int argc, char *argv[])
{
    Enable_Console_Log_Strategy();
    if (argc != 2)
    {
        cerr << "Usage: " << argv[0] << " port" << endl;
        return 1;
    }
    try
    {
        Route r;
        uint16_t port = stoi(argv[1]);
        auto tp = ThreadPool<task_t>::GetInstance();

        unique_ptr<UDPServer> server = std::make_unique<UDPServer>(port, [&r, &tp](int sockfd, const std::string &message, INETADDR &peer)
                                                                   {
        task_t t = std::bind(&Route::RouteMSaaage, &r, sockfd, message, peer);
        tp->Enqueue(t); });
        server->Init();
        server->Start();
    }
    catch (const exception &e)
    {
        cerr << "Error: " << e.what() << endl;
        return 2;
    }

    return 0;
}